## Clayton, Horrillo, and Sniderman 
## The BIAT and the AMP As Measures of Racial Prejudice in Political Science: A Methodological Assessment
## Step 1: Read and clean panel data

# Initial settings --------------------------------------------------------

rm(list = ls())
library(tidyverse)
library(readstata13)
library(haven)

# Read data ---------------------------------------------------------------

amp.panel <- read.dta13("data/anes2008_2009panel_dataset.dta") # read data


# Calculate AMP scores ----------------------------------------------------

amp.cleaned <- amp.panel %>%
  select(caseid, w9amp_ver, w10amp_ver, contains("_choice"))

w9.bw <- amp.cleaned %>% 
  filter(w9amp_ver == 2) %>% # keep black-white comparison in W9 only (not McCain-Obama)
  select(caseid, contains("w9")) %>% 
  select(-c(w9amp_ver))

col <- colnames(w9.bw) # save column names for later use

w10.bw <- amp.cleaned %>% 
  filter(w10amp_ver == 1) %>% # keep black-white comparison in W10 only (not McCain-Obama); note, coding switches
  select(caseid, contains("w10")) %>% 
  select(-c(w10amp_ver))

colnames(w9.bw) <- c(1:49)  
colnames(w10.bw) <- c(1:49)

both.bw <- rbind(w9.bw, w10.bw) # bind w9 black-white with w10 black-white
colnames(both.bw) <- col # put back in some column names 
colnames(w9.bw) <- col # w9 only analysis (appendix)

both.bw[both.bw == -5] <- NA # deal with missing data
both.bw[both.bw == -7] <- NA
both.bw[both.bw == 2] <- 0 # recode so unpleasant = 0 for more intuitive means 

w9.bw[w9.bw == -5] <- NA # deal with missing data (appendix)
w9.bw[w9.bw == -7] <- NA # (appendix)
w9.bw[w9.bw == 2] <- 0 # recode so unpleasant = 0 for more intuitive means (appendix)

black.panel <- both.bw %>% 
  select(2:25) %>% # filter black faces 
  rowMeans(na.rm = TRUE) # calculate means for each respondent, ignore NA

black.panel.w9app <- w9.bw %>% 
  select(2:25) %>% # filter black faces 
  rowMeans(na.rm = TRUE) # calculate means for each respondent, ignore NA

white.panel <- both.bw %>%
  select(26:49) %>% # filter white faces
  rowMeans(na.rm = TRUE) # calculate means for each respondent, ignore NA

white.panel.w9app <- w9.bw %>% 
  select(26:49) %>% # filter white faces 
  rowMeans(na.rm = TRUE) # calculate means for each respondent, ignore NA

with.ids <- as.data.frame(cbind(both.bw$caseid, black.panel, white.panel)) # merge back in the case id
colnames(with.ids) <- c("caseid", "black.mean", "white.mean") # add column names

with.ids <- with.ids %>%
  mutate(diff = white.mean - black.mean) %>% 
  mutate(cheater = ifelse((black.mean == 0 & white.mean == 0) | (black.mean == 1 & white.mean == 1), 1, 0))

with.ids.w9app <- as.data.frame(cbind(w9.bw$caseid, black.panel.w9app, white.panel.w9app)) # merge back in the case id
colnames(with.ids.w9app) <- c("caseid", "black.mean", "white.mean") # add column names

with.ids.w9app <- with.ids.w9app %>%
  mutate(diff = white.mean - black.mean) %>% 
  mutate(cheater = ifelse((black.mean == 0 & white.mean == 0) | (black.mean == 1 & white.mean == 1), 1, 0))


# Merge with IAT ----------------------------------------------------------

iat <- read_spss("data/ANES0809Panel_IAT.por") # IAT data 

iat.cleaned <- iat %>%
  rename(caseid = CASEID) %>% # rename vars for merge
  select(caseid, IAT_D, IAT_F0, IAT_F1, IAT_F2) %>% 
  mutate(keep = ifelse(IAT_F0 == 1 & IAT_F1 == 1 & IAT_F2 == 1, 1, 0)) 

merged <- left_join(with.ids, iat.cleaned, by = "caseid") %>% na.omit() # merge, keep complete cases only
merged.w9app <- left_join(with.ids.w9app, iat.cleaned, by = "caseid") %>% na.omit() # merge, keep complete cases only


# Clean ANES data with additional variables -------------------------------

amp.other <- amp.panel %>% 
  select(caseid,
         cpq14_01, # race
         w10d11, w10d11_warm, w10d11_cold, # black therm 
         w10d14, w10d14_warm, w10d14_cold, # white therm 
         w20m4, w20m11, # intelligent at school
         w20m7, w20m14, # lazy
         der16, # vote choice
         der08w9, # PID at wave 9
         w20l1, w20l2, w20l3, w20l4 # racial resentment
  ) %>% 
  rename(white = cpq14_01,
         vote = der16,
         pid = der08w9) %>% 
  mutate(obama = ifelse(vote == 2, 1, 0)) %>% 
  mutate(w10d11 = ifelse(w10d11 < 0, NA, w10d11),
         w10d11_warm = ifelse(w10d11_warm < 0, NA, w10d11_warm),
         w10d11_cold = ifelse(w10d11_cold < 0, NA, w10d11_cold),
         w10d14 = ifelse(w10d14 < 0, NA, w10d14),
         w10d14_warm = ifelse(w10d14_warm < 0, NA, w10d14_warm),
         w10d14_cold = ifelse(w10d14_cold < 0, NA, w10d14_cold),
         w20m4 = ifelse(w20m4 < 0, NA, w20m4),
         w20m11 = ifelse(w20m11 < 0, NA, w20m11),
         w20l1 = ifelse(w20l1 < 0, NA, w20l1),
         w20l2 = ifelse(w20l2 < 0, NA, w20l2),
         w20l3 = ifelse(w20l3 < 0, NA, w20l3),
         w20l4 = ifelse(w20l4 < 0, NA, w20l4)) %>% 
  mutate(w20l1 = 5 - w20l1,
         w20l4 = 5 - w20l4) %>% 
  mutate(rr_index = (w20l1 + w20l2 + w20l3 + w20l4) / 4)

amp.other[amp.other < 0] <- NA

amp.other <- amp.other %>% 
  mutate(whitetherm = NA,
         whitetherm = ifelse(w10d14 == 1 & w10d14_warm == 1, 7, whitetherm),
         whitetherm = ifelse(w10d14 == 1 & w10d14_warm == 2, 6, whitetherm),
         whitetherm = ifelse(w10d14 == 1 & w10d14_warm == 3, 5, whitetherm),
         whitetherm = ifelse(w10d14 == 3, 4, whitetherm),
         whitetherm = ifelse(w10d14 == 2 & w10d14_cold == 3, 3, whitetherm),
         whitetherm = ifelse(w10d14 == 2 & w10d14_cold == 2, 2, whitetherm),
         whitetherm = ifelse(w10d14 == 2 & w10d14_cold == 1, 1, whitetherm)) %>% 
  mutate(blacktherm = NA,
         blacktherm = ifelse(w10d11 == 1 & w10d11_warm == 1, 7, blacktherm),
         blacktherm = ifelse(w10d11 == 1 & w10d11_warm == 2, 6, blacktherm),
         blacktherm = ifelse(w10d11 == 1 & w10d11_warm == 3, 5, blacktherm),
         blacktherm = ifelse(w10d11 == 3, 4, blacktherm),
         blacktherm = ifelse(w10d11 == 2 & w10d11_cold == 3, 3, blacktherm),
         blacktherm = ifelse(w10d11 == 2 & w10d11_cold == 2, 2, blacktherm),
         blacktherm = ifelse(w10d11 == 2 & w10d11_cold == 1, 1, blacktherm)) %>% 
  mutate(thermdiff = whitetherm-blacktherm) %>% ## white - black (pos values = pro-white)
  mutate(lazydiff = w20m14 - w20m7, ## white - black (pos values = pro-white)
         intelldiff = w20m11 - w20m4) ## white - black (neg values = pro-white)

# Merge everything --------------------------------------------------------

## Main data analysis 

all.merged <- left_join(merged, amp.other, by = "caseid") %>% # merge, keep complete cases only
  filter(white == 1) %>% 
  filter(cheater == 0) %>% 
  filter(keep == 1)

## Appendix: W9 AMP only 

all.merged.w9app <- left_join(merged.w9app, amp.other, by = "caseid") %>% # merge, keep complete cases only
  filter(white == 1) %>% 
  filter(cheater == 0) %>% 
  filter(keep == 1)


# Obama/AMP correlation ---------------------------------------------------

cor(all.merged$diff, all.merged$obama, use = "na.or.complete")


# Save data ---------------------------------------------------------------

write.csv(all.merged, "output/main_panel.csv")
write.csv(all.merged.w9app, "output/w9app_panel.csv")
